﻿using System;
using System.Windows;
using System.Windows.Controls;
using MDCAdminTool.Controls;
using System.ComponentModel;

namespace MDCAdminTool.Commands
{
    public abstract class AddTabCommandBindingBase : AsyncCommandBindingBase
    {
        protected TabControl TabControl;
        protected TabControl InitTabControl()
        {
            if ( TabControl == null )
            {
                TabControl = Application.Current.Properties["TabControl"] as TabControl;
            }
            return TabControl;
        }

        protected void AddNewTab( object header , object content)
        {
            if( InitTabControl() == null ) return;

            try
            {
                TabControl.Dispatcher.Invoke(
                    new Action(
                        () =>
                        {
                            var tabItem = 
                                new TabItem
                                  {
                                      Header          = header,
                                      Content         = content,
                                      ContentTemplate = App.SelectDataTemplate(content)
                                  };

                            TabControl.Items.Add( tabItem );
                            TabControl.SelectedItem = tabItem;
                        }
                     ) );
            }
            catch {}
        }

        protected AddTabCommandBindingBase()
        {
            TabControl = Application.Current.Properties["TabControl"] as TabControl;
        }
    }
}
